package com.wenwu.controller.unmovable;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsNaturalEnvironment;
import com.wenwu.common.page.PageUtils;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.common.utils.Status.AttachmentFrom;
import com.wenwu.service.CommonService;
import com.wenwu.service.NaturalService;

/**
 * @project RelicsProject-web
 * @class NaturalController.java
 * @author zhujiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2015年5月13日 下午6:31:47
 * @description 自然环境
 */
@Controller(value = "NaturalController")
@RequestMapping(value = "/mgr/natural")
public class NaturalController {
	Logger log = Logger.getLogger(this.getClass());
	// 用来创建json数据
	@Autowired
	private NaturalService naturalService;
	@Autowired
	private CommonService commonService;
	private JsonGenerator jsonGenerator = null;

	@RequestMapping(value = "/init")
	public String initUserPage() {
		return "natural/naturallist";
	}

	@RequestMapping(value = "/naturalform")
	public String dictionaryform(Model model,
			@RequestParam(required = false, value = "basicId") Integer basicId,
			@RequestParam(required = false, value = "name") String name)
			throws Exception {
		log.info("进入表单页面...");
		try {
			if (StringUtils.isNotBlank(name)) {
				model.addAttribute("name", URLDecoder.decode(name, "UTF-8"));
			}
			if (basicId != null) {// 查询对应的附件集合
				List<RelicsAttachment> attachments = commonService
						.selectAttachmentListByKey(basicId, "relics_natural_environment",
								"natural_id", "basic_id",
								AttachmentFrom.RELICS_PROTECT_ENVIROMENT_01);
				model.addAttribute("attachments", attachments);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "natural/naturalform";
	}

	/**
	 * 添加不可移动文物基本信息
	 */
	@RequestMapping(value = "/addObj")
	public void addObj(RelicsNaturalEnvironment natural, HttpServletResponse response)
			throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		log.info("添加数据...");
		try {
			jsonGenerator.writeObject(naturalService.addObj(natural));
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage());
			jsonGenerator.writeObject(e.getMessage());
		}
	}

	@RequestMapping(value = "/showList")
	public @ResponseBody PaginationVO showList(RelicsNaturalEnvironment natural,
			HttpServletRequest request) throws Exception {
		PaginationVO pageVo = null;
		try {
			pageVo = PageUtils.getPaginationInfo(request);
			pageVo = naturalService.selectList(natural, pageVo);// 查询符合条件的数据
		} catch (Exception e) {
		}

		return pageVo;
	}

	@RequestMapping(value = "/showUpdate")
	public @ResponseBody RelicsNaturalEnvironment showUpdate(
			@RequestParam(required = false, value = "basicId") Integer basicId)
			throws Exception {
		RelicsNaturalEnvironment natural = null;
		try {
			if (basicId != null) {
				natural = naturalService.selectObjByKey(basicId);
			}
		} catch (Exception e) {
		}
		return natural;
	}

	@RequestMapping(value = "/showThisUpdate")
	public @ResponseBody RelicsNaturalEnvironment showThisUpdate(
			@RequestParam(required = false, value = "naturalId") Integer naturalId)
			throws Exception {
		RelicsNaturalEnvironment natural = null;
		try {
			if (naturalId != null) {
				natural = naturalService.selectThisObjByKey(naturalId);
			}
		} catch (Exception e) {
		}
		return natural;
	}

	@RequestMapping(value = "/deleteInfo")
	public void nature(HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(required = false, value = "key") String key)
			throws Exception {
		log.info("主键：" + key);
		if (key != null) {
			List<String> keys = new ArrayList<String>();
			String[] objs = key.split(",");
			for (String k : objs) {
				keys.add(k);
			}
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			try {
				int result = naturalService.deleteByKey(keys);
				jsonGenerator.writeObject("成功删除" + result + "条数据");
			} catch (Exception e) {
				jsonGenerator.writeObject("删除数据失败");
			}
		}
	}
}
